package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.Gateway;
import com.hydl.power.pojo.PowerRoom;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.GatewayService;
import com.hydl.power.service.PowerRoomService;
import com.hydl.power.service.SysUserService;
import com.hydl.power.utils.Resp;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/gateway")
@Api(value = "GatewayController", tags = "网关管理")
@CrossOrigin
public class GatewayController {
    @Autowired
    private GatewayService gatewayService;
    @Autowired
    private PowerRoomService powerRoomService;
    @Autowired
    private SysUserService sysUserService;


    // @LogAnnotation(module = "网关管理",description = "查询网关")
    @GetMapping("/queryGatewayAll")
    @ApiOperation(value="查询网关")
    public Resp queryGatewayAll(){
        return Resp.success(gatewayService.queryGatewayAll());
    }


    @LogAnnotation(module = "网关管理",description = "删除指定网关")
    @PostMapping("/deleteGatewayById")
    @ApiOperation(value="删除指定网关")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "项目自增长id",required = false,  dataType = "Integer",defaultValue = "3")
    })
    public Resp deleteGatewayById(Integer id){
        int flag=gatewayService.deleteGatewayById(id);
        if(flag==1){
            return Resp.success("Success");
        }else{
            return Resp.fail("删除失败");
        }
    }


    @Transactional
    @PostMapping("/addGateway")
    @ApiOperation(value="添加网关")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "gatewaySn", value = "网关编码",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "gatewayPwd", value = "网关密码",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "gatewayRemark", value = "备注",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "brand", value = " ",required = false,  dataType = "Integer")
    })
    public Resp addGateway(String gatewaySn, String gatewayPwd, String roomId, String projectId,String gatewayRemark,Integer brand){
        if(StringUtils.isBlank(gatewaySn) || StringUtils.isBlank(gatewayPwd) || StringUtils.isBlank(roomId) || StringUtils.isBlank(projectId)
        || StringUtils.isBlank(gatewayRemark) || StringUtils.isBlank(brand.toString())){
            return Resp.fail("参数不能为空");
        }
        int flag=gatewayService.addGateway(gatewaySn,gatewayPwd,roomId,projectId,gatewayRemark,brand);
        if( flag== 4 ){
            return Resp.success("已有此网关",null);
        }
        if( flag== 3 ){
            return Resp.success("未查询到项目编码或配电房编码",null);
        }
        if(flag == 1 ){
            return Resp.success("Success");
        }else{
            return Resp.fail("添加失败");
        }
    }



    // @LogAnnotation(module = "网关管理",description = "根据条件查询网关")
    @PostMapping("/queryByCityId")
    @ApiOperation(value="根据条件查询网关")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",required = false,  dataType = "String"),
    })
    public Resp queryByCityId(@RequestParam(required = true )Integer pageNum,
                              @RequestParam(required = true )Integer pageSize,
                              @RequestParam(required = false) String cityId,
                              @RequestParam(required = false) String projectId,
                              @RequestParam(required = false) String roomId,
                              @RequestParam(required = false) String token){
        SysUser sysUser = sysUserService.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            PageInfo<Gateway> list=gatewayService.queryByTokenId(pageNum,pageSize,cityId,projectId,roomId,token);
            return Resp.success(list);
        }
      return Resp.success(gatewayService.queryByCityId(pageNum,pageSize,cityId,projectId,roomId));
    }

}
